<template>
  <el-card class="mr-20" shadow="never">
    <div class="form-label">
      <span>{{ $t('menuStr.protocolDriverListStr') }}</span>
      <span> ( {{ $t('pageStr.protocolDriverListDesStr') }} )</span>
    </div>
    <el-table :data="form.driverList" class="mt-20" :header-cell-style="tableHeadStyle">
      <el-table-column :label="$t('pageStr.protocolDriverNameStr')" align="center" prop="_name"/>
      <el-table-column :label="$t('pageStr.protocolDriverDesStr')" align="center" prop="__text"/>
      <el-table-column :label="$t('pageStr.protocolDriverFileStr')" align="center" prop="_module"/>
      <el-table-column :label="$t('pageStr.protocolDriverVersionStr')" align="center" prop="_version"/>
      <el-table-column :label="$t('pageStr.protocolDriverAuthStatusStr')" align="center">
        <template #default="{row}">
          <div v-if="(row.needAuth && row.authResult) || !row.needAuth">{{$t('authed')}}</div>
          <div v-else-if="row.needAuth && !row.authResult">{{$t('noAuth')}}</div>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script setup>
import {ref,onBeforeMount,inject} from 'vue'
import {reqGetDriverList} from '@/api/data-collect/protocol-config'


const $x2js = inject('x2js')
const form = ref({
  driverList: [{}]
})
onBeforeMount(getData)

function getData() {
  reqGetDriverList().then((data) => {
    data=data.split("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    const driverDis = data[0];
    const driverAuthInfo = data[1];
    const driverAuthResult = data[2];
    const resp = $x2js.xml2js(driverDis);
    const needAuthArr = driverAuthInfo.match(/\nauth_file:.*\n/)[0].replace(/\n/g, "").split(":")[1].split(",");
    let authResultArr = [];
    let authAllDriversArr = [];
    if(driverAuthResult&& driverAuthResult.match(/\n\n Authorized drivers:.*\n\n/)){
      authResultArr = driverAuthResult.match(/\n\n Authorized drivers:.*\n\n/)[0].replace(/\n/g,"").split(":")[1].split(",");
      authAllDriversArr = driverAuthResult.match(/\n\nAll drivers:.*\n\n/)[0].replace(/\n/g,"").split(":")[1].split(",");
    }
    if(resp.firmwares&&resp.firmwares.item) {
      if (!resp.firmwares.item.length) {
        resp.firmwares.item = [resp.firmwares.item]
      }
      for (let index = 0; index < resp.firmwares.item.length; index++) {
        const item = resp.firmwares.item[index];
        const moduleName = item._module.replace(".so", "");
        if (needAuthArr.indexOf(moduleName) !== -1) {
          item.needAuth = 1;
        } else {
          item.needAuth = 0;
        }
        if (authResultArr.indexOf(moduleName) !== -1) {
          item.authResult = 1;
        } else {
          item.authResult = 0;
        }
        if (authAllDriversArr.indexOf(moduleName) === -1 || authResultArr.indexOf(" __edpskernel__") === -1) {
          item.needAuth = 1;
          item.authResult = 0;
        }
      }
    }
    console.log(resp)
    form.value.driverList = resp.firmwares.item
  })
}
const tableHeadStyle = {
  'text-align': 'center',
  'background-color': '#F5EFE9',
  'max-width': '1000px'
}
</script>

<style scoped>
.form-label span:nth-child(2) {
  color: #717289;
  font-weight: 400;
  font-size: 14px;
}

.form-label {
  font-size: 17px;
  font-weight: bold;
  color: #161940;
}
</style>